﻿namespace ScrumLite.WebUx.Tests
{
    using System;
    using System.Collections.Specialized;
    using System.Security.Principal;
    using System.Web;
    using System.Web.Mvc;
    using ScrumLite.Services.Contracts;
    using Rhino.Mocks;
    using System.Web.Routing;

    public static class MockHelpers
    {
        internal static readonly Guid KnownProjectId = new Guid("12184E41-B84D-4c2f-9251-CB0F8E8D7DDB");
        internal static readonly Deliverable DeliverableMock = new Deliverable { Name = "Name", Url = new Uri("http://www.mock.com"), ReleasedOn = DateTime.Now };

        internal static MockRepository SetupMocks(Controller controller)
        {
            MockRepository mocks = new MockRepository();
            MockView mockView = new MockView();

            HttpContextBase context = mocks.DynamicMock<HttpContextBase>();
            HttpRequestBase request = mocks.DynamicMock<HttpRequestBase>();
            HttpResponseBase response = mocks.DynamicMock<HttpResponseBase>();
            HttpServerUtilityBase server = mocks.DynamicMock<HttpServerUtilityBase>();
            HttpSessionStateBase session = mocks.DynamicMock<HttpSessionStateBase>();
            
            SetupResult.For(context.Request).Return(request);
            SetupResult.For(request.Form).Return(new NameValueCollection());            
            SetupResult.For(context.Response).Return(response);
            SetupResult.For(context.Server).Return(server);
            SetupResult.For(context.Session).Return(session);
            SetupResult.For(context.User).Return(new GenericPrincipal(new GenericIdentity("Foo"), null));

            mocks.ReplayAll();

            RouteData routeData = new RouteData();
            controller.GetType().GetProperty("TempData").SetValue(controller, new TempDataDictionary(context), null);
            ControllerContext contextMock = new ControllerContext(context, routeData, controller);
            controller.ControllerContext = contextMock;
            controller.ViewEngine = mockView;

            return mocks;
        }
    }
}
